package com.example.administrator.idocment.docment;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.provider.MediaStore;

import com.example.administrator.idocment.BaseTypeFind;
import com.example.administrator.idocment.msgpack.modelpack.AviModel;
import com.example.administrator.idocment.tooltime.TimerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/15 0015.
 */
public class AviFind extends BaseTypeFind<AviModel>
{
    public static final String[] aviType=new String[]{"video/3gpp","video/x-ms-asf","video/x-msvideo",
            "video/vnd.mpegurl","video/x-m4v","video/quicktime","video/mp4","video/mpeg",
    };

    public AviFind(Context context) {
        super(context);
    }
    List<AviModel> aviModels=new ArrayList<>();
    @Override
    public void select() {
        Uri uri= MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        String[] colums=new String[]{MediaStore.Video.Media.DATA,MediaStore.Video.Media.DISPLAY_NAME,MediaStore.Video.Media.DURATION};
        String where="";
        try {

        String sort=MediaStore.Video.Media.DATE_TAKEN+" DESC";
        Cursor cursor = contentResolver.query(uri, colums, where, null, sort);
        cursor.moveToFirst();
        do {
            String path=cursor.getString(0);
            String title =  cursor.getString(1);
            Long duration=cursor.getLong(2);
            String playDuration= TimerFactory.create(TimerFactory.TVFormat).paseTimer(duration);

            AviModel aviModel=new AviModel();
            aviModel.setPath(path);
            aviModel.setTitle(title);
            aviModel.setDuration(playDuration);
            aviModel.setFirstThumb(getVideoThumbnail(path));
            aviModels.add(aviModel);
            android.util.Log.e("aviModel","path:"+path+" title:"+title+" duration:"+playDuration);
        }while (cursor.moveToNext());

            cursor.close();
        }catch (Exception e)
        {
            android.util.Log.e("aviModel",e.toString());
        }

    }

    public static Bitmap getVideoThumbnail(String filePath) {
        Bitmap bitmap = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            retriever.setDataSource(filePath);
            bitmap = retriever.getFrameAtTime();
        }
        catch(IllegalArgumentException e) {
            e.printStackTrace();
        }
        catch (RuntimeException e) {
            e.printStackTrace();
        }
        finally {
            try {
                retriever.release();
            }
            catch (RuntimeException e) {
                e.printStackTrace();
            }
        }
        return bitmap;
    }


    @Override
    public List<AviModel> getListInfo() {
        return aviModels;
    }


}
